package com.love.zywanandroid.customerview

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.love.zywanandroid.enums.BottomTab
import com.zat.mes.andrmes.R
import kotlinx.android.synthetic.main.bottom_view.view.*

/**
 * @author zy
 * @date :2019-08-22 11:03
 * @description: 底部导航
 */
class BottomView : LinearLayout, View.OnClickListener {
    /**
     * 点击回调
     */
    private lateinit var listener: ((BottomTab) -> Unit)

    fun setListener(listener: (BottomTab) -> Unit) {
        this.listener = listener
    }

    /**
     * 点击事件处理
     */
    override fun onClick(v: View?) {
        when (v) {
            ll_tab_home -> {
                listener?.invoke(BottomTab.HOME)
                setBottomViewStyle(BottomTab.HOME)
            }

            ll_tab_me -> {
                listener?.invoke(BottomTab.ME)
                setBottomViewStyle(BottomTab.ME)
            }


        }
    }

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context?, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {

    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        ll_tab_home.setOnClickListener(this)
        ll_tab_me.setOnClickListener(this)


    }

    /**
     * 设置选中style
     */
    fun setBottomViewStyle(p: BottomTab) {
        val nomalColor = ContextCompat.getColor(context, R.color.color_text_grey2)
        val selectColor = ContextCompat.getColor(context, R.color.color_theme)
        val homeColor = if (p == BottomTab.HOME) selectColor else nomalColor
        val systemColor = if (p == BottomTab.SYSTEM) selectColor else nomalColor
        val collectColor = if (p == BottomTab.COLLECT) selectColor else nomalColor
        val meColor = if (p == BottomTab.ME) selectColor else nomalColor
        tv_tab_home.setTextColor(homeColor)
        tv_tab_me.setTextColor(meColor)
        val homeDraw =
            if (p == BottomTab.HOME) R.mipmap.icon_gongzuoxuanzhong else R.mipmap.icon_gongzuoweixuanzhong
        val meDraw =
            if (p == BottomTab.ME) R.mipmap.icon_wodexuanzhong else R.mipmap.icon_wodeweixuanzhong
        iv_tab_home.setImageResource(homeDraw)
        iv_tab_me.setImageResource(meDraw)

    }

}